package com.yanqu.road.dao.impl.yrank;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.rank.yrank.UnionYRank;
import com.yanqu.road.entity.union.UnionBaseInfo;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UnionYRankDaoImpl extends TempDataObjectDao<UnionYRank> {

    public static String getUnionYRankSql = "select * from `t_c_yrank_union` where `activityId`=?";

    public Map<Integer, Map<String, Map<String, UnionYRank>>> getUnionYRankMap(int activityId) {

        Map<Integer, Map<String, Map<String, UnionYRank>>> map = new ConcurrentHashMap<>();

        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getUnionYRankSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                UnionYRank parse = parse(rs);
                map.putIfAbsent(parse.getGroupId(), new ConcurrentHashMap<>());
                map.get(parse.getGroupId()).putIfAbsent(parse.getIdKey(), new ConcurrentHashMap<>());
                map.get(parse.getGroupId()).get(parse.getIdKey()).put(parse.getUnionUid(), parse);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateUnionYRankSql = "update `t_c_yrank_union` set unionBaseInfo=?,value=?,param=?,updateTime=?,posMap=? where activityId=? and groupId=? and idKey=? and unionUid=?";

    @Override
    protected boolean updateData(UnionYRank unionYRank) {
        unionYRank.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(unionYRank.getUnionBaseInfo())));
        para.put(i++, new DbParameter(Types.VARCHAR, unionYRank.getValue().toString()));
        para.put(i++, new DbParameter(Types.VARCHAR, unionYRank.getParam()));
        para.put(i++, new DbParameter(Types.BIGINT, unionYRank.getUpdateTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(unionYRank.getPosMap())));
        para.put(i++, new DbParameter(Types.INTEGER, unionYRank.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, unionYRank.getGroupId()));
        para.put(i++, new DbParameter(Types.VARCHAR, unionYRank.getIdKey()));
        para.put(i++, new DbParameter(Types.VARCHAR, unionYRank.getUnionUid()));
        if (getDb().executeNoneQuery(updateUnionYRankSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUnionYRankSql = "insert into `t_c_yrank_union`(activityId,groupId,idKey,unionUid,unionBaseInfo,value,param,updateTime,posMap) values (?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(UnionYRank unionYRank) {
        unionYRank.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, unionYRank.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, unionYRank.getGroupId()));
        para.put(i++, new DbParameter(Types.VARCHAR, unionYRank.getIdKey()));
        para.put(i++, new DbParameter(Types.VARCHAR, unionYRank.getUnionUid()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(unionYRank.getUnionBaseInfo())));
        para.put(i++, new DbParameter(Types.VARCHAR, unionYRank.getValue().toString()));
        para.put(i++, new DbParameter(Types.VARCHAR, unionYRank.getParam()));
        para.put(i++, new DbParameter(Types.BIGINT, unionYRank.getUpdateTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(unionYRank.getPosMap())));
        if (getDb().executeNoneQuery(insertUnionYRankSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UnionYRank parseData(ResultSet rs) throws SQLException {
        UnionYRank unionYRank = new UnionYRank();
        unionYRank.setActivityId(rs.getInt("activityId"));
        unionYRank.setGroupId(rs.getInt("groupId"));
        unionYRank.setIdKey(rs.getString("idKey"));
        unionYRank.setUnionUid(rs.getString("unionUid"));
        unionYRank.setUnionBaseInfo(JSON.parseObject(rs.getString("unionBaseInfo"), new TypeReference<UnionBaseInfo>() {
        }));
        unionYRank.setValue(new BigInteger(rs.getString("value")));
        unionYRank.setParam(rs.getString("param"));
        unionYRank.setUpdateTime(rs.getLong("updateTime"));
        unionYRank.setPosMap(JSON.parseObject(rs.getString("posMap"), new TypeReference<ConcurrentHashMap<Long, Integer>>() {
        }));
        return unionYRank;
    }

}
